/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-17 下午4:10:24          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TelnetPortCollector extends Collector {

    private static Logger logger = Logger.getLogger(TelnetPortCollector.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "Conn_Success" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Origin_Ip", "Target_Ip", "Target_Port", "Time_Out" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        String[] propMetas = getPropMeta();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader buffReader = null;
        String originIp = this.getParamMap().get("Origin_Ip");
        String targeIp = this.getParamMap().get("Target_Ip");
        String targePort = this.getParamMap().get("Target_Port");
        String timeOut = this.getParamMap().get("Time_Out");
        int time = 5;
        if (timeOut != null && timeOut.matches("\\d+")) {
            time = Integer.valueOf(timeOut);
        }
        try {
            if (originIp != null && !originIp.equals("")) {
                ip = originIp;
            }
            //第一次
            //perl -e 'use IO::Socket;if($socket=IO::Socket::INET->new (PeerAddr=>"172.168.2.210",PeerPort=>"80",Type=>SOCK_STREAM,Proto=>"tcp",))
            //{print "1\n";}else{print "0\n";}'
            //第二次，增加超时设置
            //perl -e 'use IO::Socket;$socket=IO::Socket::INET->new (PeerAddr=>"172.31.17.191",PeerPort=>"22",Type=>SOCK_STREAM,Proto=>"tcp",Timeout=>5)or die "0\n";print "1\n";$socket.close();'
            //String cmd = "perl -e 'use IO::Socket;if($socket=IO::Socket::INET->new (PeerAddr=>\"" + targeIp + "\" ,PeerPort=>\"" + targePort + "\",Type=>SOCK_STREAM,Proto=>\"tcp\",)){print \"1\";}else{print \"0\";}'";
            String cmd = "perl -e 'use IO::Socket;$socket=IO::Socket::INET->new (PeerAddr=>\"" + targeIp + "\",PeerPort=>\"" + targePort + "\",Type=>SOCK_STREAM,Proto=>\"tcp\",Timeout=>" + time + ")or die \"0\\n\";print \"1\\n\";$socket.close();'";
            buffReader = CommandReader.getReader(ip, cmd, 0, 0);
            String tmpStr = "";
            while ((tmpStr = buffReader.readLine()) != null) {
                if (tmpStr.trim().startsWith("1")) {
                    propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
                } else {
                    propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
                }
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } finally {
            if (buffReader != null) {
                try {
                    buffReader.close();
                } catch (IOException e) {
                    logger.error("failed to close buffReader", e);
                }
            }
        }
        return values;
    }
}
